package com.ekuaitu.kuaitu.activity;

import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.ViolRecordBean;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jieyaozu on 2017/3/27.
 * 违规记录
 */

public class ViolationRecordedActivity extends BaseActivity {
    @BindView(R.id.refresh_listview)
    PullToRefreshListView refreshLayout;
    @BindView(R.id.recorded_message)
    LinearLayout tvMessage;
    private ListView mListView;
    private RecordedAdapter recordedAdapter;
    private int currentIndex = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_violation_recorded;
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
        mListView = refreshLayout.getRefreshableView();
        recordedAdapter = new RecordedAdapter();
        mListView.setAdapter(recordedAdapter);
        refreshLayout.setMode(PullToRefreshBase.Mode.BOTH);//两端刷新
        refreshLayout.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentIndex = 1;
                loadData(currentIndex);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentIndex++;
                loadData(currentIndex);
            }
        });

        new Handler().postDelayed(new Runnable() {
            public void run() {
                refreshLayout.setRefreshing();
            }
        }, 500);
    }

    private void loadData(final int pageIndex) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getViolRecordedInfo(MyApplication.getInstance().getRequestFieldMap(), pageIndex + "", "8").enqueue(new Callback<ViolRecordBean>() {
            @Override
            public void onResponse(Call<ViolRecordBean> call, Response<ViolRecordBean> response) {
                refreshLayout.onRefreshComplete();
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus().equals("200")) {
                        List<ViolRecordBean.AttachmentBean> recordedBeanList = response.body().getAttachment();
                        if (pageIndex == 1) {
                            recordedAdapter.clearData();
                        }
                        recordedAdapter.addData(recordedBeanList);
                        if ((recordedBeanList == null || recordedBeanList.size() == 0) && pageIndex == 1) {
                            tvMessage.setVisibility(View.VISIBLE);
                        } else {
                            tvMessage.setVisibility(View.GONE);
                        }
                    } else {
                        ToastCompat.makeText(ViolationRecordedActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ViolRecordBean> call, Throwable t) {
                refreshLayout.onRefreshComplete();
                ToastCompat.makeText(ViolationRecordedActivity.this, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.return_viol_recored})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.return_viol_recored:
                finish();
                break;
        }
    }

    public class RecordedAdapter extends BaseAdapter {
        private List<ViolRecordBean.AttachmentBean> recordedBeanList = new ArrayList<>();

        public void clearData() {
            if (recordedBeanList != null) {
                recordedBeanList.clear();
            }
        }

        public void addData(List<ViolRecordBean.AttachmentBean> recordedBeanList) {
            if (recordedBeanList != null && recordedBeanList.size() > 0) {
                this.recordedBeanList.addAll(recordedBeanList);
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return recordedBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(ViolationRecordedActivity.this, R.layout.item_listview_violation, null);
            TextView tvTime = (TextView) view.findViewById(R.id.violation_time);
            TextView tvContent = (TextView) view.findViewById(R.id.violation_content);
            ViolRecordBean.AttachmentBean recordedBean = recordedBeanList.get(position);
            tvTime.setText(recordedBean.getDate());
            tvContent.setText(recordedBean.getMessage());
            return view;
        }
    }
}
